package com.qianfeng.missfood;

import android.app.Application;

import com.baidu.mapapi.SDKInitializer;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig;
import com.lidroid.xutils.cache.MD5FileNameGenerator;
import com.qianfeng.missfood.bean.User;
import com.qianfeng.missfood.constants.AppConfig;
import com.qianfeng.missfood.db.DBHelper;
import com.qianfeng.missfood.utils.FileUtils;
import com.qianfeng.missfood.utils.LocationHelper;
import com.qianfeng.missfood.utils.PopupHelper;

/**
 * Created by Administrator on 15-8-17.
 */
public class BaseApp extends Application {


    private static BaseApp app;
    private boolean netState;
    private DbUtils dbUtils;
    private User user;
    private boolean loginState;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public boolean getLoginState() {

        return loginState;
    }

    public void setLoginState(boolean loginState) {

        this.loginState = loginState;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        initBitmapGolbalConfig();
        //百度地图初始化
        SDKInitializer.initialize(this);
        LocationHelper.init(this);
        LocationHelper.getInstance().start();

        PopupHelper.init(this);//初始化PopupWindow的工具类

        initDbUtils();

    }

    public void initDbUtils() {

        if (dbUtils == null) {

            DbUtils.DaoConfig config = new DbUtils.DaoConfig(this);
            config.setDbDir(this.getCacheDir() + AppConfig.DB_PATH);
            config.setDbVersion(AppConfig.DB_VERSION);
            config.setDbName(AppConfig.DB_NAME);
            config.setDbUpgradeListener(new DbUtils.DbUpgradeListener() {
                @Override
                public void onUpgrade(DbUtils dbUtils, int oldVersion, int newVersion) {

                    if (oldVersion < newVersion) {

                        DBHelper.DbUser.getDbUtils(dbUtils);
                        DBHelper.DbUser.deleteTab();
                        DBHelper.DbUser.createTab();

                    }
                }
            });

            dbUtils = DbUtils.create(config);
            DBHelper.DbUser.getDbUtils(dbUtils);
        }
    }

    public static BaseApp getInstance() {

        return app;
    }

    public boolean isNetState() {
        return netState;
    }

    public void setNetState(boolean netState) {
        this.netState = netState;
    }

    private void initBitmapGolbalConfig() {
        BitmapGlobalConfig config = BitmapGlobalConfig.getInstance(this, FileUtils.getImageCache());

        config.setDiskCacheEnabled(true);
        //设置内存的大小 默认 4m 最小2m
        config.setMemoryCacheSize(8 * 1024 * 1024);
        config.setDiskCacheSize(1024 * 1024 * 100);
        //设置线程池的线程数量  默认 5
        config.setThreadPoolSize(4);
        //保存图片的名字以md5命名
        config.setFileNameGenerator(new MD5FileNameGenerator());

    }


}
